//@ts-nocheck
/**
现象：
setN的时候 函数里的n并不会发生改变
<App>会重新刷新
每次<App>刷新 useState出来的n的值都不一样(如果是因为setN导致的刷新)

脑补:
setN一定会修改数据x,将n+1存入x
setN一定会触发<App>重新渲染
useState肯定会从x读取n的最新值
每个组件有自己的数据x,我们将其命名为state
*/
import React from 'react';
import ReactDOM from 'react-dom';
const rootElement = document.getElementById('root');

const render = () => {
  ReactDOM.render(<App/>, rootElement)
};

let _state;

const myUseState = initialValue => {
  console.log('myUseState run');
  _state = _state === undefined ? initialValue : _state;
  const setState = newValue => {
    _state = newValue;
    render()
  };
  return [_state, setState];
};

function App(){
  const [n, setN] = myUseState(0);

  return (
    <div className="App">
      <p>{n}</p>
      <p>
        <button onClick={() => setN(n + 1)}>+1</button>
      </p>
    </div>
  );
}

ReactDOM.render(<App/>, rootElement);
